package cn.com.xy.sms.sdk.net.util;
/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2015-10-12 17:17:14
 ******************************************************************************/





import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class CommandAnalyzer {

   

    /**
     * 命令执行者
     * @author javiylee
     */
    public static interface Executor {

        public void exeCmd(Command cmd);
    }

    static String catchTargetRegex = "#(\\d+)\\{([^}]+)\\}";

    static Pattern catchTargetPattern = Pattern.compile(catchTargetRegex);

    /**
     * 命令解析
     * @param commandString
     * @return
     */
    public static List<Command> analyze(String commandString) {
        if (commandString == null || commandString.trim().length() == 0)
            return null;
        Matcher m = catchTargetPattern.matcher(commandString);
        boolean finded = m.find();
        List<Command> commands = null;
        if (finded) {
            commands = new ArrayList<Command>();
            do {
                String target = m.group(1);
                String res = m.group(2);
                commands.add(new Command(Integer.valueOf(target), res));
            } while (m.find());
        }

        return commands;
    }

    public static void main(String[] args) {
        String commandString = "#0{-noWait} #4{-noWait -wait=10} #10{-ids=1,2,3 -domain=http://bizport.cn/newservice} #11{-sql=asfa dfa sdff}";
        // 解析命令，支持多个命令解析
        List<Command> commands = analyze(commandString);
        // 创建一个命令执行者
        CommandPrinter executor = new CommandPrinter();
        for (Command cmd : commands) {
            executor.exeCmd(cmd);
        }
    }

    public static class CommandPrinter implements Executor {

        @Override
        public void exeCmd(Command cmd) {
        }
    }

}
